import * as THREE from "three";
import { ThreeHelper } from "@/src/ThreeHelper";
import { MethodBaseSceneSet, LoadGLTF } from "@/src/ThreeHelper/decorators";
import { MainScreen } from "./Canvas";
import type { GLTF } from "three/examples/jsm/loaders/GLTFLoader";
import { Injectable } from "@/src/ThreeHelper/decorators/DI";
import type { GUI } from "dat.gui";
import gsap from "gsap";

@Injectable
// @ThreeHelper.useWebGPU
export class Main extends MainScreen {
    static instance: Main;

    constructor(private helper: ThreeHelper) {
        super(helper);
        helper.main = this;
        this.init();
        Main.instance = this;
    }

    @MethodBaseSceneSet({
        addAxis: true,
        cameraPosition: new THREE.Vector3(0, 1, 2),
        cameraTarget: new THREE.Vector3(0, 0, 0),
        useRoomLight: false,
        near: 0.1,
        far: 800,
    })
    init() {
        this.helper.renderer.setPixelRatio(window.devicePixelRatio);
        // this.helper.renderer.setPixelRatio(2);

        this.addLight();

        // this.loadModel();

        this.handleScene();
    }

    handleScene() {
        const floor = this.helper.create.plane(5, 5).add();

        floor.material(
            new THREE.MeshStandardMaterial({
                color: new THREE.Color("#ccc"),
            })
        );

        floor.mesh.rotateX(Math.PI / -2)
    }

    @LoadGLTF("/public/models/martelo.glb")
    loadModel(gltf?: GLTF) {
        if (gltf) {
            this.helper.add(gltf.scene);
        }
    }

    @ThreeHelper.InjectAnimation(Main)
    animation() {}

    @ThreeHelper.AddGUI(Main)
    createEnvTexture(gui: GUI) {
        // gui.addFunction(async () => {}, "1");
    }

    addLight() {
        const light = new THREE.PointLight(0xffffff, 150, 200, 2);
        light.position.set(10, 10, 0);

        this.helper.scene.add(light);

        light.castShadow = true;
        light.shadow.mapSize.width = 1024 * 2;
        light.shadow.mapSize.height = 1024 * 2;
        light.shadow.camera.near = 0.3;
        light.shadow.camera.far = 200;
        light.shadow.bias = -0.0012;
        light.shadow.intensity = 1000;

        const shadowFolder = this.helper.gui?.addFolder("shadow");
        shadowFolder?.add(light.shadow, "intensity", 0, 100).name("强度💪");
        shadowFolder?.add(light.shadow, "bias").name("偏移量");
        shadowFolder?.add(light, "intensity", 0, 1000).name("点光源强度");

        this.helper.renderer.shadowMap.enabled = true;
        // this.helper.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
        this.helper.renderer.shadowMap.type = THREE.BasicShadowMap;

        const skyColor = 0xffffff; // light blue
        const groundColor = 0x000000; // brownish orange
        const intensity = 2;
        const hemisphereLight = new THREE.HemisphereLight(skyColor, groundColor, intensity);
        this.helper.scene.add(hemisphereLight);
    }
}
